import Vue from "vue";
import VueRouter from "vue-router";
import login from "../views/Login.vue";
import Home from "../views/Home.vue";
// 导入三个子页面
import RoleManage from "../views/RoleManage.vue";
import MenuManage from "../views/MenuManage.vue";
import UserManage from "../views/UserManage.vue";

Vue.use(VueRouter);

// 配置路由
const routes = [
  {
    path: "/",
    name: "login",
    component: login,
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    // 子路由
    children:[
      {
        path: "/roleManage",
        name: "角色管理",
        component: RoleManage,
      },
      {
        path: "/menuManage",
        name: "菜单管理",
        component: MenuManage,
      },
      {
        path: "/userManage",
        name: "用户管理",
        component: UserManage,
      },
    ]
  },
 
];

const router = new VueRouter({

  routes,
});


// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登录
router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next();
  } else {
    let token = localStorage.getItem('Authorization');
    if (token === null || token === '') {
      next('/');
    } else {
      next();
    }
  }
});

export default router;
